package com.supermap.localization.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.supermap.localization.api.SampleComponent;
import com.supermap.localization.commontypes.SampleInfo;
import com.supermap.localization.util.ConfigsUtil;

public class PropertiesSampleComponent implements SampleComponent {

    private static Logger logger = Logger.getLogger(PropertiesSampleComponent.class);

    @Override
    public void saveSample(List<SampleInfo> sampleInfos) {
        Path path = Paths.get(ConfigsUtil.extractConfig.getExtractFilePath());
        Path locationInfoPath = Paths.get(ConfigsUtil.extractConfig.getExtractInfoFilePath());
        BufferedWriter sampleBw = null;
        BufferedWriter locationInfoBw = null;
        try {
            Files.deleteIfExists(path);
            Files.createFile(path);
            Files.deleteIfExists(locationInfoPath);
            Files.createFile(locationInfoPath);
            sampleBw = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
            locationInfoBw = Files.newBufferedWriter(locationInfoPath, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
            Iterator<SampleInfo> iterator = sampleInfos.iterator();
            while (iterator.hasNext()) {
                SampleInfo sampleInfo = iterator.next();
                sampleBw.write(sampleInfo.format());
                sampleBw.newLine();
                locationInfoBw.write(sampleInfo.formatLocation());
                locationInfoBw.newLine();
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (sampleBw != null) {
                    sampleBw.close();
                }
                if (locationInfoBw != null) {
                    locationInfoBw.close();
                }
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public Map<String, SampleInfo> readSampleInfos(String path) {
        Map<String, SampleInfo> result = new HashMap<>();
        Path sampleFile = Paths.get(path);
        BufferedReader br = null;
        try {
            br = Files.newBufferedReader(sampleFile, StandardCharsets.UTF_8);
            String line = null;
            while ((line = br.readLine()) != null) {
                String tmp[] = StringUtils.split(line, "=");
                SampleInfo info = new SampleInfo();
                info.setKey(tmp[0]);
                info.setValue(tmp[1]);
                result.put(info.getKey(), info);
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                throw new RuntimeException(e);
            }
        }
        return result;
    }

    @Override
    public void saveSampleTranslate(List<SampleInfo> sampleInfos) {
        Path path = Paths.get(ConfigsUtil.replaceConfig.getTranslateFilePath());
        BufferedWriter sampleTranslateBw = null;
        try {
            Files.deleteIfExists(path);
            Files.createFile(path);
            sampleTranslateBw = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
            Iterator<SampleInfo> iterator = sampleInfos.iterator();
            while (iterator.hasNext()) {
                SampleInfo sampleInfo = iterator.next();
                sampleTranslateBw.write(sampleInfo.formatTranslate());
                sampleTranslateBw.newLine();
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (sampleTranslateBw != null) {
                    sampleTranslateBw.close();
                }
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public List<SampleInfo> readSampleTranslate(String path) {
        List<SampleInfo> result = new ArrayList<>();
        Path sampleFile = Paths.get(path);
        BufferedReader br = null;
        try {
            br = Files.newBufferedReader(sampleFile, StandardCharsets.UTF_8);
            String line = null;
            while ((line = br.readLine()) != null) {
                String tmp[] = StringUtils.split(line, "=");
                SampleInfo info = new SampleInfo();
                info.setKey(tmp[0]);
                info.setTranslate(tmp[1]);
                result.add(info);
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                throw new RuntimeException(e);
            }
        }
        return result;
    }

}
